package main

import (
	"fmt"
	"syscall"
	"unsafe"
)

type TestStr struct {
	sa string
	ia int
	fa float32
	ba bool
}
type TestInter interface {
	varShow()
	listShow()
	testNullParams()
	testOneParam()
}
func (ts TestStr)varShow () {
	/** 变量申明 */
	ts.sa = "test"
	ts.fa  = 2.32
	ts.ia  = 10
	ts.ba  = false
	fmt.Println(ts.sa, ts.fa, ts.ia, ts.ba)
}
func (ts TestStr)listShow() {
	/** 数组 结构体 range */
	var tracks = []*TestStr{
		{"K1", 3, 2.21, true},
		{"K2", 9, 0.01, false},
		{"K3", 53, 9.99, false},
		{"K4", 11, 1.66, true},
	}
	for k,v:= range tracks{
		fmt.Println(k,v.sa,v.fa,v.ia,v.ba)
	}
}
func testNullParams()  {
	fmt.Println("test make a function without params")
}
func testOneParam(a string) {
	fmt.Printf("test make a function with one param %s \n", a)
}
func testSum(a int) {
	fmt.Println("testSum start")
	for i:=0; i<a; i++ {
		a --
		fmt.Printf("%d < %d \n", i, a)
	}
}
func testAddress()  {
	a := 10
	b := 10
	c := a
	d := &a
	fmt.Printf("a = %d address is %x\n", a, &a)
	fmt.Printf("b = %d address is %x\n", b, &b)
	fmt.Printf("c = %d address is %x\n", c, &c)
	fmt.Printf("d = %d address is %p and %x\n", *d, &d, d)
}
func testSlice()  {
	s :=[]int{2,3,4,5,6,7}
	s1 := make([]int, len(s))
	copy(s1,s)
	fmt.Println(s1)
	var a=make([]byte,20)
	copy(a[12:],"cbd")
	fmt.Println(a)
	// 使用了省略号(…)来自动展开切片
	a=append(a,"sss"...)
	fmt.Println(a)
}
func winSound()  {
	funInDllFile, err := syscall.LoadLibrary("Winmm.dll") // 调用dll文件
	if err != nil {
		print("cant not call : syscall.LoadLibrary , errorInfo :" + err.Error())
	}

	defer syscall.FreeLibrary(funInDllFile)
	// 调用dll里面的函数
	funName := "PlaySound"
	// 注册一长串调用代码，简化为 _win32Fun 变量.
	win32Fun, err := syscall.GetProcAddress(syscall.Handle(funInDllFile), funName)
	// 通过syscall.Syscall6()去调用win32的xxx函数，因为xxx函数有3个参数,故需取Syscall6才能放得下. 最后的3个参数,设置为0即可
	_, _, err = syscall.Syscall6(
		uintptr(win32Fun),                                                                             // 调用的函数名
		3,                                                                                                     // 指明该函数的参数数量
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("alert") ) ),                    // 该函数的参数1. 可通过msdn查找函数名 查参数含义
		// SystemStart
		uintptr( 0 ),                                                                                       // 该函数的参数2.
		uintptr( 0 ),                                                                                       // 该函数的参数3.
		0,
		0,
		0)

}
func main()  {
	tes := &TestStr{"1", 3, 2.21, true}
	tes.varShow()
	tes.listShow()
	testNullParams()

	tString := "Paul"
	testOneParam(tString)
	testSum(3)
	testAddress()
	testSlice()
}